package com.itheima.sc.impl.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.sc.api.ums.UmsAdminRoleRelationApi;
import com.itheima.sc.mapper.ums.UmsAdminRoleRelationMapper;
import com.itheima.sc.pojo.ums.UmsAdminRoleRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.*;
import java.util.List;

@DubboService
public class UmsAdminRoleRelationServiceImpl implements UmsAdminRoleRelationApi {
    @Autowired
    private UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    /**
     * 通过adminId获取roleId
     */
    @Override
    public List<UmsAdminRoleRelation> getRoleIds(Integer adminId) {
        LambdaQueryWrapper<UmsAdminRoleRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsAdminRoleRelation::getAdminId, adminId);
        return umsAdminRoleRelationMapper.selectList(wrapper);
    }

    /**
     * 先将之前的数据删除
     */
    @Override
    public void delete(Integer adminId) {
        LambdaQueryWrapper<UmsAdminRoleRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsAdminRoleRelation::getAdminId, adminId);
        umsAdminRoleRelationMapper.delete(wrapper);
    }

    /**
     * 重新给用户分配角色
     */
    @Override
    public void roleUpdate(UmsAdminRoleRelation umsAdminRoleRelation) {
        umsAdminRoleRelationMapper.insert(umsAdminRoleRelation);
    }

    /**
     * 根据adminId查询roleId
     *
     * @param adminId
     * @return
     */
    @Override
    public List<UmsAdminRoleRelation> getByAdminId(Long adminId) {
        LambdaQueryWrapper<UmsAdminRoleRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(adminId != null, UmsAdminRoleRelation::getAdminId, adminId);
        return umsAdminRoleRelationMapper.selectList(wrapper);
    }
}
